package com.telit.firecommand.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telit.common.constant.Constant;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.firecommand.entity.CommFireHandle;
import com.telit.firecommand.service.FireHandleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2021/8/4 9:27
 */
@RestController
@RequestMapping(value = "/fire-handle")
@Api(tags = "指挥信息", value = "/fire-handle", produces = MediaType.APPLICATION_JSON_VALUE)
public class FireHandleController {

    @Autowired
    FireHandleService fireHandleInfoService;

    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "新增指挥信息", httpMethod = Constant.REQUEST_METHOD_POST)
    public DataReturn add(@RequestBody CommFireHandle commFireHandle) {
        commFireHandle.setCreateTime(new Date());
        commFireHandle.setUpdateTime(new Date());
        fireHandleInfoService.add(commFireHandle);
        return DataReturn.ok();
    }

    @PostMapping(value = "/listByPage/{current}/{size}", produces = "application/json;charset=UTF-8")
    @ApiOperation("分页条件指挥信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "path", dataType = "int")
    })
    public DataReturn listByPage(@PathVariable("current") Integer current,
                                 @PathVariable("size") Integer size,
                                 @RequestBody(required = false) CommFireHandle fireHandleInfo) {
        IPage<CommFireHandle> fireHandleInfoIPage = fireHandleInfoService.selectByPage(current, size, fireHandleInfo);
        return DataReturn.ok(
                new PageData<>((int) fireHandleInfoIPage.getTotal(), current, size, fireHandleInfoIPage.getRecords()));
    }

    @PostMapping(value = "/list", produces = "application/json;charset=UTF-8")
    @ApiOperation("无分页条件查询指挥信息")
    public DataReturn list(@RequestBody(required = false) CommFireHandle fireHandleInfo) {
        List<CommFireHandle> fireHandleInfoList = fireHandleInfoService.selectByNoPage(fireHandleInfo);
        return DataReturn.ok(fireHandleInfoList);
    }

    @GetMapping(value = "/getInfoByFireId/{fireId}", produces = "application/json;charset=UTF-8")
    @ApiOperation("根据火情id查询指挥信息")
    public DataReturn getInfoByFireId(@PathVariable("fireId") Long fireId) {
        CommFireHandle fireHandleInfo = fireHandleInfoService.selectInfoByfireId(fireId);
        return DataReturn.ok(fireHandleInfo);
    }

    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    @ApiOperation("修改指挥信息")
    public DataReturn update(@RequestBody CommFireHandle fireHandleInfo) {
        fireHandleInfo.setUpdateTime(new Date());
        fireHandleInfoService.updateData(fireHandleInfo);
        return DataReturn.ok();
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除指挥信息")
    public DataReturn delete(@PathVariable("id") String id) {

        fireHandleInfoService.deleteById(id);

        return DataReturn.ok();
    }

}
